var prefix = "/psPerson/manage";
$().ready(function() {
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function onTypeChange() {
	validateRule();
}

function save() {
	var username = $.trim($('#username').val());
	if(!username){
	   layer.msg("司机姓名不能为空");
	   return;
	}
	var code = $.trim($('#code').val());
	if(!code){
	   layer.msg("编号不能为空");
	   return;
	}
	var mobile = $.trim($('#mobile').val());
	if(!mobile){
	   layer.msg("手机号码不能为空");
	   return;
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/update",
		data : $('#carForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code === 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				if (data.msg) {
					parent.layer.alert(data.msg)
				}else {
					window.location.href = "/";
				}
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#carForm").validate({
		rules: {
			username: {
				required: true
			},
			code: {
				required: true
			},
			mobile: {
				required: true
			},
			qq: {
				required: true
			},
			address: {
				required: true
			},
			storeroom: {
				required: true
			},
			storeroomAddress: {
				required: true
			},
			province: {
				required: true
			},
			city: {
				required: true
			}
		},
		messages: {
			username: {
				required: icon + "司机姓名不能为空"
			},
			code: {
				required: icon + "编号不能为空"
			},
			mobile: {
				required: icon + "手机号码不能为空"
			},
			qq: {
				required: icon + "QQ/微信不能为空"
			},
			address: {
				required: icon + "联系地址不能为空"
			},
			storeroom: {
				required: icon + "仓库名不能为空"
			},
			storeroomAddress: {
				required: icon + "仓库地址不能为空"
			},
			province: {
				required: icon + "配送省不能为空"
			},
			city: {
				required: icon + "配送市不能为空"
			}
		}
	})
}

function chooseXiaoQus() {
	var province = $('#province').val();
	var city = $('#city').val();
	var carId = $('#id').val();
	if(!province){
		layer.msg("请选择配送省份");
		return;
	}
	if(!city){
		layer.msg("请选择配送城市");
		return;
	}
	layer.open({
		id:"choose_chooseXiaoQus",
		type:2,
		title:"小区选择" ,
		maxmin: true,
		shadeClose: true,
		area : [ '60%', '98%' ],
		content: "/carxq/manage/chooseXiaoQu?province" + province +"&city="+city +"&carId=" + carId
	})
}



function loadXiaoQuInfo(xqNames,mids) {
	$('#xiaoqu').val(xqNames);
	$('#xqs').val(xqNames);
	$('#mids').val(mids);
}

function clearXiaoqu() {
	$('#xiaoqu').val("");
}

function chooseWarehouse() {
	layer.open({
		id:"choose_warehouse",
		type:2,
		title:"选择仓库" ,
		maxmin: true,
		shadeClose: true,
		area : [ '90%', '80%' ],
		content: "/warehouse/manage/chooseWarehouse"
	})
}

function loadWarehouseInfo(id, name, address) {
	$('#storeroomId').val(id);
	$('#storeroom').val(name);
	$('#storeroomAddress').val(address);
}